//
//  LBEvaluationResultVC.m
//  V-Learn
//
//  Created by Apple on 2019/1/1.
//  Copyright © 2019 yxk. All rights reserved.
//

#import "LBEvaluationResultVC.h"
#import "LBPayVC.h"
#import "WKOnlinePingguResultModel.h"
#import "LBOnlineVC.h"
#import "LBCoureseDetailVC.h"
#import "WKOnlinePingguResultModel.h"
@interface LBEvaluationResultVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UITextView *textView;
@end

@implementation LBEvaluationResultVC
- (UITextView *)textView{
    if (!_textView) {
        _textView = [[UITextView alloc] init];
    }
    return _textView;
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(59*kScaleFit, 70*kScaleFit, ScreenW-59*2, 300*kScaleFit) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    
    [self requestData];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)requestData{
    //支付成功后返回一个ordernum  保存后用来 展示结果使用
    if (self.dataArray) {
        [self.tableView reloadData];
    }else{
        NSString *token = [UserModel getUserModel].token;
        NSMutableDictionary *data = [NSMutableDictionary dictionary];
        [data setObject:token forKey:@"token"];
        NSString *orderNum = [[NSUserDefaults standardUserDefaults] objectForKey:@"orderNum"];
        if (orderNum==nil) {
            
        }else{
            [data setObject:orderNum forKey:@"orderNum"];
        }
        
        if (orderNum==nil) {
            [WYHXNetWorkManager get:WKQueryPlanInfoByOrderNum_WKURL params:data completionHandler:^(id responseObj, NSError *error) {
                NSString *code = responseObj[@"code"];
                if ([code integerValue]==200) {
                    self.dataArray = [NSMutableArray array];
                    self.dataArray = [WKOnlinePingguResultModel mj_objectArrayWithKeyValuesArray:responseObj[@"data"]];
                    
//                    NSLog(@"%@",responseObj);
                }
                [self.tableView reloadData];
            }];
        }else{
            [self.tableView reloadData];
        }

    }

    

}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    self.navigationItem.title = @"生成结果";
    // Do any additional setup after loading the view.
    [self requestOnlineResult];

}
- (void)requestOnlineResult{
    [WYHXNetWorkManager get:WKQueryOnLinePlanResult_WKURL params:@{} completionHandler:^(id responseObj, NSError *error) {
        NSString *code = responseObj[@"code"];
        NSString *msg = responseObj[@"msg"];
        if ([code integerValue]==200) {
            NSString *data = responseObj[@"data"];
            self.textView.text = data;
        }else if([code integerValue]==500){
            [MBProgressHUD showSuccess:msg toView:self.view];
            return ;
        }
        [self.tableView reloadData];
    }];
}


- (void)setupUI{
    UIView *topView = [UIView new];
    [self.view addSubview:topView];
    topView.frame = CGRectMake(0, 0, ScreenW, 32*kScaleFit+LBNAVBAR_HEIGHT);
    [self.view addSubview:topView];
    
    UIImageView *topImageView = [[UIImageView alloc] init];
    topImageView.image = [UIImage imageNamed:@"wk_top-bg"];
    topImageView.frame = CGRectMake(0, 0, ScreenW,32*kScaleFit+LBNAVBAR_HEIGHT);
    [topView addSubview:topImageView];
    
    UIButton * btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    btn.frame = CGRectMake(17*kScaleFit, 35*kScaleFit, 20*kScaleFit, 27*kScaleFit);
//    btn.backgroundColor = [UIColor redColor];
    [btn addTarget:self action:@selector(BackAction) forControlEvents:(UIControlEventTouchUpInside)];
    [btn setImage:[UIImage imageNamed:@"back"] forState:(UIControlStateNormal)];
    [btn setImage:[UIImage imageNamed:@"back"] forState:(UIControlStateHighlighted)];
    [topView addSubview:btn];
    
    UILabel * titleLab = [UILabel labelWithText:@"评估结果" andTextColor:rgb(255,255,255) andName:nil andFontSize:18*kScaleFit];
    [topView addSubview:titleLab];
    [titleLab makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(topView);
        make.top.mas_equalTo(37*kScaleFit);

    }];
    UIImageView *backImageView=[[UIImageView alloc]initWithFrame:self.tableView.bounds];
    [backImageView setImage:[UIImage imageNamed:@"wk_Result-bg"]];
    
    UIView *header = [UIView new];
    header.backgroundColor = [UIColor orangeColor];
    header.frame = CGRectMake(0, 0, self.tableView.frame.size.width, 0.01);
    self.tableView.tableHeaderView = header;
    
//    UIView *view = [UIView new];
//    view.backgroundColor = rgb(255,191,159);
//    [self.tableView addSubview:view];
//    view.frame = CGRectMake(0, 0, self.tableView.frame.size.width, 33);
    
//
//    UIView *view1 = [UIView new];
//    view1.backgroundColor = [UIColor clearColor];//rgb(255,191,159);
//    view1.frame = CGRectMake(0, 0, (ScreenW-(59*2)*kScaleFit)/2, 33*kScaleFit);
//    [view addSubview:view1];
//
//    UILabel *label1= [UILabel labelWithText:@"年龄" andTextColor:rgb(230,84,0) andName:nil andFontSize:12*kScaleFit];
//    [view1 addSubview:label1];
//    [label1 makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.centerY.mas_equalTo(view1);
//    }];
//
//    UIView *view2 = [UIView new];
//    view2.backgroundColor = rgb(255,191,159);
//    view2.frame = CGRectMake((ScreenW-(59*2)*kScaleFit)/2, 0, (ScreenW-(59*2)*kScaleFit)/2, 33*kScaleFit);
//    [view addSubview:view2];
//
//    UILabel *label2= [UILabel labelWithText:@"课程名称" andTextColor:rgb(230,84,0) andName:nil andFontSize:12*kScaleFit];
//    [view2 addSubview:label2];
//    [label2 makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.centerY.mas_equalTo(view2);
//    }];
    
    
//    [backImageView addSubview:view];
    self.tableView.backgroundView=backImageView;
//    self.tableView.contentOffset = CGPointMake(59*kScaleFit, -150*kScaleFit);
    
    [self.view addSubview:self.tableView];
//    [self.tableView setContentOffset:CGPointMake(0, -150) animated:YES];
    [self.view addSubview:self.textView];
    
    [self.textView makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.tableView.mas_bottom).mas_equalTo(22*kScaleFit);
        make.left.mas_equalTo(30*kScaleFit);
        make.right.mas_equalTo(-30*kScaleFit);
        make.height.mas_equalTo(88*kScaleFit);
    }];
    self.textView.text = @"";
    self.textView.editable = NO;
    self.textView.textColor = rgb(40,40,40);
    
    UIButton *shoppingButton = [[UIButton alloc] init];
    [self.view addSubview:shoppingButton];
    shoppingButton.layer.cornerRadius = 22;
    shoppingButton.layer.masksToBounds = YES;
    [shoppingButton makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15*kScaleFit);
        make.right.mas_equalTo(-15*kScaleFit);
        make.height.mas_equalTo(44*kScaleFit);
        make.bottom.mas_equalTo(-54*kScaleFit);
    }];
    [shoppingButton setTitle:@"重新规划" forState:UIControlStateNormal];
    [shoppingButton setTitleColor:rgb(255,255,255) forState:UIControlStateNormal];
    shoppingButton.titleLabel.font = LBFont(18);
//    shoppingButton.backgroundColor = rgb(20,94,183);
    [shoppingButton setBackgroundImage:LBImage(@"wk_pay_button_blue") forState:UIControlStateNormal];
    [shoppingButton addTarget:self action:@selector(clickShoppingBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *chongxinButton = [[UIButton alloc] init];
    [self.view addSubview:chongxinButton];
    chongxinButton.layer.cornerRadius = 22;
    chongxinButton.layer.masksToBounds = YES;
    [chongxinButton makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15*kScaleFit);
        make.right.mas_equalTo(-15*kScaleFit);
        make.height.mas_equalTo(44*kScaleFit);
        make.bottom.mas_equalTo(shoppingButton.mas_top).mas_equalTo(-24*kScaleFit);
    }];
    [chongxinButton setTitle:@"立即购买" forState:UIControlStateNormal];
    [chongxinButton setTitleColor:rgb(255,255,255) forState:UIControlStateNormal];
    chongxinButton.titleLabel.font = LBFont(18);
//    chongxinButton.backgroundColor = rgb(233,84,18);
    [chongxinButton setBackgroundImage:LBImage(@"wk_shadow_btn") forState:UIControlStateNormal];
    [chongxinButton addTarget:self action:@selector(clickGouMaiBtn:) forControlEvents:UIControlEventTouchUpInside];
    

}

- (void)BackAction{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 在线
- (void)clickShoppingBtn:(UIButton *)sender{

    LBOnlineVC *onlineVC = [[LBOnlineVC alloc] init];
    [self.navigationController pushViewController:onlineVC animated:YES];
}
#pragma mark - 立即购买
- (void)clickGouMaiBtn:(UIButton *)sender{
    LBCoureseDetailVC *courseDetailVC = [[LBCoureseDetailVC alloc] init];
    if (self.dataArray) {
        WKOnlinePingguResultModel *model = self.dataArray[0];
        if (model.courseId) {
            courseDetailVC.courseId = model.courseId;
            courseDetailVC.type = @"1";
//            courseDetailVC.courseInfo = model.courseInfo;
            [self.navigationController pushViewController:courseDetailVC animated:YES];
        }
    }
    

    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSeparatorStyleNone;
        cell.backgroundColor = [UIColor clearColor];
        UIView *view = [UIView new];
        view.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:view];
        UIView *view1 = [UIView new];
        view1.backgroundColor = [UIColor clearColor];
        view1.frame = CGRectMake(0, 0, (ScreenW-(59*2)*kScaleFit)/2, 44*kScaleFit);
        [view addSubview:view1];
        
        UILabel *label1= [UILabel labelWithText:@"3岁1个月" andTextColor:rgb(51,51,51) andName:nil andFontSize:12*kScaleFit];
        WKOnlinePingguResultModel *model = self.dataArray[indexPath.row];
        label1.text = [NSString stringWithFormat:@"%@岁%@月",model.infoAgeYear,model.infoAgeMonth];
        [view1 addSubview:label1];
        [label1 makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.mas_equalTo(view1);
        }];
        
        UIView *view2 = [UIView new];
        view2.backgroundColor = [UIColor clearColor];
        view2.frame = CGRectMake((ScreenW-(59*2)*kScaleFit)/2, 0, (ScreenW-(59*2)*kScaleFit)/2, 44*kScaleFit);
        [view addSubview:view2];
        
        UILabel *label2= [UILabel labelWithText:@"双语课程第一季" andTextColor:rgb(51,51,51) andName:nil andFontSize:12*kScaleFit];
        label2.text = model.courseName;
        [view2 addSubview:label2];
        [label2 makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.mas_equalTo(view2);
        }];
        
        UIView *lineView = [UIView new];
        lineView.backgroundColor =  rgb(254,204,179);
        [cell.contentView addSubview:lineView];
        [lineView makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(10);
            make.right.mas_equalTo(-10);
            make.bottom.mas_equalTo(0);
            make.height.mas_equalTo(1);
        }];
    }

    
    
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];//rgb(255,191,159);
    
    UIView *view1 = [UIView new];
    view1.backgroundColor = [UIColor clearColor];//rgb(255,191,159);
    view1.frame = CGRectMake(30, 0, (ScreenW-(59*2)*kScaleFit)/2-30, 33*kScaleFit);
    [view addSubview:view1];
    
    UILabel *label1= [UILabel labelWithText:@"年龄" andTextColor:rgb(230,84,0) andName:nil andFontSize:12*kScaleFit];
    [view1 addSubview:label1];
    [label1 makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(view1).mas_equalTo(-18*kScaleFit);
        make.top.mas_equalTo(14*kScaleFit);
    }];
    
    UIView *view2 = [UIView new];
    view2.backgroundColor = [UIColor clearColor];//rgb(255,191,159);
    view2.frame = CGRectMake((ScreenW-(59*2)*kScaleFit)/2, 0, (ScreenW-(59*2)*kScaleFit)/2, 33*kScaleFit);
    [view addSubview:view2];
    
    UILabel *label2= [UILabel labelWithText:@"课程名称" andTextColor:rgb(230,84,0) andName:nil andFontSize:12*kScaleFit];
    [view2 addSubview:label2];
    [label2 makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(view2);
        make.top.mas_equalTo(14*kScaleFit);
    }];
    
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 33*kScaleFit;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
